#include <bits/stdc++.h>
#define ll long long
const int mxN=2e5;
using namespace std;
// int dx[8]={-1,0,0,1,-1,-1,1,1};
// int dy[8]={0,-1,1,0,1,-1,1,-1};
int T;
string s;
int n,q;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>T;
for(int i=0;i<T;i++){
cin>>s>>q;
n=s.size();
int c[n+2];
memset(c,0,sizeof(c));
for(int cx=0;cx<n;cx++){
if(s[cx]=='['||s[cx]==']') c[cx+2]++;
}
for(int z=2;z<=n+1;z++) c[z]+=c[z-2];
for(int j=0;j<q;j++){
int l,r;cin>>l>>r;
--l,--r;
int a=c[r+1]-c[l];
int b=c[r+2]-c[l+1];
cout<<abs(b-a)<<endl;
}
}
return 0;
}
933A - A Twisty Movement | 1722F - L-shapes |
1196B - Odd Sum Segments | 1325D - Ehab the Xorcist |
552B - Vanya and Books | 1722E - Counting Rectangles |
168A - Wizards and Demonstration | 168B - Wizards and Minimal Spell |
7A - Kalevitch and Chess | 912B - New Year's Eve |
1537C - Challenging Cliffs | 879B - Table Tennis |
1674E - Breaking the Wall | 1282A - Temporarily unavailable |
1366C - Palindromic Paths | 336A - Vasily the Bear and Triangle |
926A - 2-3-numbers | 276D - Little Girl and Maximum XOR |
1253C - Sweets Eating | 1047A - Little C Loves 3 I |
758D - Ability To Convert | 733A - Grasshopper And the String |
216A - Tiling with Hexagons | 1351B - Square |
1225A - Forgetting Things | 1717A - Madoka and Strange Thoughts |
1717B - Madoka and Underground Competitions | 61B - Hard Work |
959B - Mahmoud and Ehab and the message | 802G - Fake News (easy) |